package com.platform.modules.webService.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.activiti.engine.impl.util.json.JSONException;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.entity.SysWeChatUserEntity;
import com.platform.modules.sys.service.SysUserService;
import com.platform.modules.sys.service.SysWeChatUserService;
import com.platform.modules.webService.WeChatUserWebService;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;

import org.apache.commons.lang3.StringUtils;
/**
 * 用于微信调用接口
 * @author wangLc
 */
@Service("weChatUserWebService")
//@WebService(endpointInterface="com.platform.modules.webService.WeChatUserWebService", serviceName="WeChatUserWebService")
@WebService(targetNamespace="http://webService.modules.platform.com/",endpointInterface = "com.platform.modules.webService.WeChatUserWebService")
public class WeChatUserWebServiceImpl  implements WeChatUserWebService{
  @Resource
  private WebServiceContext wsContext;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysWeChatUserService sysWeChatUserService;
  
   /**
    * 1.无此人员。2.密码错误。3.正常。ERROR。异常
   */

  public String findWeChatUser(String username, String password) {

	  JSONArray arr = new JSONArray();
	  JSONObject per  = new JSONObject();
	  String result = "";
	  SysUserEntity user = sysUserService.queryByUserName(username);
	  if(user != null){
		  if(user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())){
			  per.put("result", "SUCCESS");
			  per.put("name", user.getRealName());                    //登陆人姓名
			  per.put("username", user.getUserName()); 			      //用户  staff_mobile
			  per.put("password", password);                          //密码
			  per.put("staff_id", user.getUserId());                  //建设云Id
			  per.put("staff_mobile", user.getContact());              //手机号
			  per.put("staff_email", user.getEmail());                 //邮箱
			  per.put("staff_card", user.getUserCard());              //身份证号
			  per.put("CHECK_PROF", "");                              //专业分类
			  per.put("userType", user.getAccountType());             //用户类别
			  per.put("comp_name", "");                               //公司名称
			  per.put("comp_orgcode", "");                            //组织机构代码
			  per.put("comp_id", "");                                  //组织Id
			  result = "SUCCESS";
		  }else{
			  result = "ERROR";
		  }
	  }else{
		  result = "NO";
	  }
	    per.put("result", result);
		arr.add(per);
		return arr.toString();
  	}
  
  /**
   * 注册人员信息
   */
  public String saveWeChatUser(String xjjsuser){
    try{
        System.err.println("xjjsuser===="+xjjsuser);
        JSONObject jsonObject = JSONObject.parseObject(xjjsuser);
//      String stemFrom = jsonObject.getString("stem_from");
//      String weixinAccountid = jsonObject.getString("weixin_accountid");
        String staffId = jsonObject.getString("staff_id");
        String staffName = jsonObject.getString("staff_name");
        String openId = jsonObject.getString("open_id");
        String username = jsonObject.getString("username");
        String regionId = jsonObject.getString("region_id");
        String weixinAccountId = jsonObject.getString("weixin_accountid");
        
        
        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("userId", staffId);
	        params.put("openId", openId);
	        params.put("userName", username);
        List<SysWeChatUserEntity> list = sysWeChatUserService.queryAll(params);
        if(list != null && list.size()>0){
        	for(SysWeChatUserEntity sysWeChatUserEntity:list){
              if(StringUtils.isNotBlank(staffId) && StringUtils.isNotBlank(openId)){
                  sysWeChatUserEntity.setUserId(staffId);
                  sysWeChatUserEntity.setRealName(staffName);			                            //人名
//                  xjjsuser1.setStaffMobile(jsonObject.getString("STAFF_MOBILE"));               //手机号码    
                  sysWeChatUserEntity.setStaffEmail(jsonObject.getString("staff_email"));       //邮箱
                  sysWeChatUserEntity.setStaffCard(jsonObject.getString("staff_card"));         //身份证号码
                  sysWeChatUserEntity.setRoleType(jsonObject.getString("role_type"));           //角色类型
                  sysWeChatUserEntity.setCreditCode(jsonObject.getString("comp_orgcode"));     //企业组织机构
                  sysWeChatUserEntity.setOrgName(jsonObject.getString("comp_name"));           //企业名称
                  sysWeChatUserEntity.setOpenId(openId);                                         //用户公众号Id
                  sysWeChatUserEntity.setRegionId(regionId);           //地区编号
                  sysWeChatUserEntity.setUserName(username);            //用户名
                  sysWeChatUserEntity.setPassWord("");                                           //密码
                  sysWeChatUserEntity.setRegionId("2");
                  sysWeChatUserEntity.setCheckProf(jsonObject.getString("check_prof"));         //专业分类
                  sysWeChatUserEntity.setStemFrom("7");                                          //系统来源
                  sysWeChatUserEntity.setCreateTime(new Date());       
                  sysWeChatUserEntity.setWeixinAccountid(weixinAccountId);                     //微信公众号Id
                  sysWeChatUserService.save(sysWeChatUserEntity);
                  System.err.println("更新人员信息");
              }
        	}
        }else{
        	SysWeChatUserEntity sysWeChatUserEntity = new SysWeChatUserEntity();
            sysWeChatUserEntity.setUserId(staffId);
            sysWeChatUserEntity.setRealName(staffName);			                            //人名
//            xjjsuser1.setStaffMobile(jsonObject.getString("STAFF_MOBILE"));               //手机号码    
            sysWeChatUserEntity.setStaffEmail(jsonObject.getString("staff_email"));       //邮箱
            sysWeChatUserEntity.setStaffCard(jsonObject.getString("staff_card"));         //身份证号码
            sysWeChatUserEntity.setRoleType(jsonObject.getString("role_type"));           //角色类型
            sysWeChatUserEntity.setCreditCode(jsonObject.getString("comp_orgcode"));     //企业组织机构
            sysWeChatUserEntity.setOrgName(jsonObject.getString("comp_name"));             //企业名称
            sysWeChatUserEntity.setOpenId(openId);                                         //用户公众号Id
            sysWeChatUserEntity.setRegionId(regionId);                                      //地区编号
            sysWeChatUserEntity.setUserName(username);                                       //用户名
            sysWeChatUserEntity.setPassWord("");                                           //密码
            sysWeChatUserEntity.setRegionId("2");
            sysWeChatUserEntity.setCheckProf(jsonObject.getString("check_prof"));         //专业分类
            sysWeChatUserEntity.setStemFrom("7");                                          //系统来源
            sysWeChatUserEntity.setCreateTime(new Date());       
            sysWeChatUserEntity.setWeixinAccountid(weixinAccountId);                     //微信公众号Id
            sysWeChatUserService.save(sysWeChatUserEntity);
            System.err.println("保存人员信息");
        }
        
    }catch (JSONException e){
      e.printStackTrace();
      return "";
    }
    return "";
  }

}
